package com.ruoyi.wms.service;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.wms.common.enu.ReceiptStatusTypeEnum;
import com.ruoyi.wms.common.enu.ShipmentStatusTypeEnum;
import com.ruoyi.wms.common.enu.ShipmentTypeEnum;
import com.ruoyi.wms.domain.bo.StaticBo;
import com.ruoyi.wms.domain.entity.stock.PurchaseRequests;
import com.ruoyi.wms.domain.entity.stock.ReceiptOrder;
import com.ruoyi.wms.domain.entity.stock.ShipmentOrder;
import com.ruoyi.wms.domain.vo.StatisticsVo;
import com.ruoyi.wms.mapper.PurchaseRequestsMapper;
import com.ruoyi.wms.mapper.ReceiptOrderMapper;
import com.ruoyi.wms.mapper.ShipmentOrderMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 统计Service接口
 */
@RequiredArgsConstructor
@Service
public class StatisticsService {

    private final PurchaseRequestsMapper purchaseRequestsMapper;

    private final ReceiptOrderMapper receiptOrderMapper;

    private final ShipmentOrderMapper shipmentOrderMapper;

    /**
     * 统计入库，出库，采购订单
     *
     * @return
     */
    public StatisticsVo statistics(Date startDate,Date endDate) {
        StatisticsVo statisticsVo = new StatisticsVo();
        startDate = DateUtil.beginOfDay(startDate);
        //结束时间
        endDate = DateUtil.endOfDay(endDate);
        //采购订单统计
        List<PurchaseRequests> requests = purchaseRequestsMapper.selectList(Wrappers.<PurchaseRequests>lambdaQuery()
            .between(PurchaseRequests::getRequestDate, startDate, endDate));
        if (!requests.isEmpty()) {
            statisticsVo.setPurchaseOrderCount(requests.size());
            AtomicReference<BigDecimal> purchaseOrderAmount = new AtomicReference<>(BigDecimal.ZERO);
            requests.forEach(item -> {
                BigDecimal amount = item.getPayableAmount();
                if (amount != null) {
                    purchaseOrderAmount.set(purchaseOrderAmount.get().add(amount));
                }
            });
            statisticsVo.setPurchaseOrderAmount(purchaseOrderAmount.get());
        }
        //入库统计
        List<ReceiptOrder> receiptOrders = receiptOrderMapper.selectList(Wrappers.<ReceiptOrder>lambdaQuery()
            .eq(ReceiptOrder::getReceiptOrderStatus, ReceiptStatusTypeEnum.FINISH.getValue())
            .between(ReceiptOrder::getCreateTime, startDate, endDate));
        if (!receiptOrders.isEmpty()) {
            statisticsVo.setInCount(receiptOrders.size());
            AtomicReference<BigDecimal> inAmount = new AtomicReference<>(BigDecimal.ZERO);
            receiptOrders.forEach(item -> {
                if (item.getPayableAmount() != null) {
                    inAmount.set(inAmount.get().add(item.getPayableAmount()));
                }
            });
            statisticsVo.setInAmount(inAmount.get());
        }
        //出库统计
        List<ShipmentOrder> shipmentOrders = shipmentOrderMapper.selectList(Wrappers.<ShipmentOrder>lambdaQuery()
            .eq(ShipmentOrder::getShipmentOrderStatus, ShipmentStatusTypeEnum.FINISH.getValue())
            .between(ShipmentOrder::getCreateTime, startDate, endDate));
        if (!shipmentOrders.isEmpty()) {
            statisticsVo.setOutCount(shipmentOrders.size());
            AtomicReference<BigDecimal> outAmount = new AtomicReference<>(BigDecimal.ZERO);
            shipmentOrders.forEach(item -> {
                if (item.getReceivableAmount() != null) {
                    outAmount.set(outAmount.get().add(item.getReceivableAmount()));
                }
            });
            statisticsVo.setOutAmount(outAmount.get());
        }
        //销售订单数量
        List<ShipmentOrder> orderList = shipmentOrderMapper.selectList(Wrappers.<ShipmentOrder>lambdaQuery()
            .eq(ShipmentOrder::getShipmentOrderStatus, ShipmentStatusTypeEnum.FINISH.getValue())
            .eq(ShipmentOrder::getShipmentOrderType, ShipmentTypeEnum.SALE.getValue())
            .between(ShipmentOrder::getCreateTime, startDate, endDate));
        if (!orderList.isEmpty()) {
            statisticsVo.setSaleOrderCount(orderList.size());
            AtomicReference<BigDecimal> saleOrderAmount = new AtomicReference<>(BigDecimal.ZERO);
            orderList.forEach(item -> {
                    if (item.getReceivableAmount() != null) {
                        saleOrderAmount.set(saleOrderAmount.get().add(item.getReceivableAmount()));
                    }
                }
            );
            statisticsVo.setSaleOrderAmount(saleOrderAmount.get());
        }
        return statisticsVo;
    }
}
